<template>
  <el-card class="sku-card">
    <el-form
      label-width="100px"
      ref="skuFormRef"
      :model="skuFormData"
      :rules="skuFormRules"
    >
      <el-form-item label="SPU名称"> xxx </el-form-item>
      <el-form-item label="SKU名称" prop="skuName">
        <el-input placeholder="请输入SKU名称" v-model="skuFormData.skuName" />
      </el-form-item>
      <el-form-item label="价格(元)" prop="price">
        <el-input-number
          class="sku-input-number"
          :min="0"
          :max="100000000000"
          controls-position="right"
          placeholder="请输入价格"
          v-model="skuFormData.price"
        />
      </el-form-item>
      <el-form-item label="重量(kg)" prop="weight">
        <el-input-number
          class="sku-input-number"
          :min="0"
          :max="10000000"
          controls-position="right"
          placeholder="请输入重量"
          v-model="skuFormData.weight"
        />
      </el-form-item>
      <el-form-item label="规格描述" prop="skuDesc">
        <el-input
          placeholder="请输入SKU规格描述"
          type="textarea"
          :row="2"
          v-model="skuFormData.skuDesc"
        />
      </el-form-item>
      <el-form-item label="平台属性" prop="skuAttrValueList">
        <el-row :gutter="10" class="sku-wrap">
          <el-col
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            :xl="4"
            v-for="(attr, index) in attrList"
            :key="attr.id"
          >
            <div class="sku-attr-wrap">
              <span class="sku-attr-name">{{ attr.attrName }}</span>
              <el-select v-model="skuFormData.skuAttrValueList[index]">
                <el-option
                  v-for="attrValue in attr.attrValueList"
                  :key="attrValue.id"
                  :label="attrValue.valueName"
                  :value="`${attr.id}:${attr.attrName}:${attrValue.id}:${attrValue.valueName}`"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="销售属性" prop="skuSaleAttrValueList">
        <el-row :gutter="10" class="sku-wrap">
          <el-col
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            :xl="4"
            v-for="(saleAttr, index) in spuSaleAttrList"
            :key="saleAttr.saleAttrName"
          >
            <div class="form-col">
              <span class="sku-attr-name">{{ saleAttr.saleAttrName }}</span>
              <el-select v-model="skuFormData.skuSaleAttrValueList[index]">
                <el-option
                  v-for="saleAttrValue in saleAttr.spuSaleAttrValueList"
                  :key="saleAttrValue.id"
                  :label="saleAttrValue.saleAttrValueName"
                  :value="`${saleAttr.id}:${saleAttr.saleAttrName}:${saleAttrValue.id}:${saleAttrValue.saleAttrValueName}`"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="图片列表" prop="skuImageList">
        <el-table
          :data="spuImageList"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" />
          <el-table-column label="图片">
            <template v-slot="{ row }">
              <img :src="row.imgUrl" :alt="row.imgName" class="sku-img" />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-tag type="success" size="small" v-if="row.isDefault === '1'">
                默认
              </el-tag>
              <el-button
                type="primary"
                size="small"
                v-else
                @click="setSkuDefaultImg(row)"
              >
                设置默认
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="goAttrList">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SaveSku",
};
</script>

<script lang="ts" setup>
import { getAttrListApi } from "@/api/product/attrList";
import type {
  SpuFormData,
  SpuImageList,
  SpuSaleAttrList,
} from "@/api/product/model/spuModel";
import type { AttrList } from "@/api/product/model/attrListModel";
import { getSpuSaleAttrListApi, getSpuImageListApi } from "@/api/product/spu";
import {ElMessage} from "element-plus"
import { useCategoryStore } from "@/stores/category";
import { onMounted, ref, reactive } from "vue";
import { saveSkuApi } from "@/api/product/sku";
const emit=defineEmits(["showSpuList"])
const props = defineProps<{
  spu: SpuFormData;
}>();
const categoryStore = useCategoryStore();
const attrList = ref<AttrList>([]);
const spuSaleAttrList = ref<SpuSaleAttrList>([]);
const spuImageList = ref<SpuImageList>([]);
onMounted(async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;
  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
});

onMounted(async () => {
  spuImageList.value = await getSpuImageListApi(props.spu.id);
});

onMounted(async () => {
  spuSaleAttrList.value = await getSpuSaleAttrListApi(props.spu.id);
});
//定义表单数据
const skuFormRef = ref<FormInstance>();
const skuFormData = reactive({
  skuName: "",
  price: 0,
  weight: 0,
  skuDesc: "",
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuImageList: [],
  skuDefaultImg: "",
});
//表单验证规则
const skuImageListValidator = (rule: any, value: any, callback: any) => {
  if (!value.length) {
    callback("请至少选择一张图片");
    return;
  }

  if (!skuFormData.skuDefaultImg) {
    callback("请至少选择一张默认图片");
    return;
  }

  callback();
};
//表单验证规则
const skuFormRules = reactive<FormRules>({
  skuName: [{ required: true, message: "请输入SKU名称", trigger: "blur" }],
  price: [{ required: true, message: "请输入SKU价格", trigger: "blur" }],
  weight: [{ required: true, message: "请输入SKU重量", trigger: "blur" }],
  skuDesc: [{ required: true, message: "请输入SKU规则描述", trigger: "blur" }],
  skuAttrValueList: [{ required: true, message: "请选择SKU平台属性" }],
  skuSaleAttrValueList: [{ required: true, message: "请选择SKU销售属性" }],
  skuImageList: [{ required: true, validator: skuImageListValidator }],
});
const handleSelectionChange = (val) => {
  skuFormData.skuImageList = val;
};
const setSkuDefaultImg = (row) => {
  spuImageList.value.forEach((img) => {
    img.isDefault = "0";
  });
  row.isDefault = "1";
  if (!skuFormData.skuDefaultImg) {
    skuFormRef.value?.clearValidate(["skuImageList"]);
  }
  skuFormData.skuDefaultImg = row.imgUrl;
};

const submitForm = async () => {
  await skuFormRef.value?.validate();
  await saveSkuApi({
    ...skuFormData,
    skuAttrValueList: skuFormData.skuAttrValueList
      .filter(Boolean)
      .map((item) => {
        const [attrId, attrName, valueId, valueName] = item.split(":");
        return {
          attrId: +attrId,
          attrName,
          valueId: +valueId,
          valueName,
        };
      }),
    skuSaleAttrValueList: skuFormData.skuSaleAttrValueList
      .filter(Boolean)
      .map((saleAttr) => {
        const [saleAttrId, saleAttrName, saleAttrValueId, saleAttrValueName] =
          saleAttr.split(":");
        return {
          saleAttrId: +saleAttrId,
          saleAttrName,
          saleAttrValueId: saleAttrValueId,
          saleAttrValueName: +saleAttrValueName,
        };
      }),

    spuId: props.spu.id,
    category3Id: categoryStore.category3Id,
  });
  ElMessage.success('SKU修改成功')
  emit('showSpuList',1)

};
const goAttrList=()=>{
  emit('showSpuList',1)
}
</script>

<style scoped>
.form-col {
  margin-bottom: 10px;
}
.sku-img {
  width: 150px;
}
</style>
